package com.jtoon.servlet;

import java.io.IOException;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.jtoon.bean.Bookmark;
import com.jtoon.bean.Comics;
import com.jtoon.bean.Episode;
import com.jtoon.util.PMF;

public class BookmarkServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		PersistenceManager pm = PMF.get().getPersistenceManager();
		HttpSession session = request.getSession();
		
		long bid = request.getParameter("bid") == null ? 0 : Long.valueOf(request.getParameter("bid"));
		long cid = request.getParameter("cid") == null ? 0 : Long.valueOf(request.getParameter("cid"));
		long eid = request.getParameter("eid") == null ? 0 : Long.valueOf(request.getParameter("eid"));
		int eOrder = request.getParameter("eOrder") == null ? 0 : Integer.valueOf(request.getParameter("eOrder"));
		int imgNum = request.getParameter("imgNum") == null ? 0 : Integer.valueOf(request.getParameter("imgNum"));
		String mode = request.getParameter("mode") == null ? "" : request.getParameter("mode");
		
		Query query = pm.newQuery(Bookmark.class);
		  
		if(session.getAttribute("id") != null){
			 
			try{ 
				request.setAttribute("isLogin", true);
				 
				if(mode.equals("add")){
					Comics comics = (Comics)pm.getObjectById(Comics.class, cid);
					Episode episode = (Episode)pm.getObjectById(Episode.class, eid);
					
					String uid = session.getAttribute("id").toString();
					  
					Bookmark bookmark = new Bookmark(uid, cid, eid, eOrder, imgNum, comics.getTitle(), episode.getTitle());
					pm.makePersistent(bookmark);
					
					response.sendRedirect("/bookmark");
				}else if(mode.equals("del")){
					Bookmark bookmark = pm.getObjectById(Bookmark.class, bid);
					pm.deletePersistent(bookmark);
					response.sendRedirect("/bookmark");
				}else{
					
					String uid = session.getAttribute("id").toString();
					
					query.setFilter("uid == uidParam");
					query.declareParameters("String uidParam"); 
					List<Bookmark> bookmarkList = (List<Bookmark>)query.execute(uid);
					request.setAttribute("bookmarkList", bookmarkList);
					request.setAttribute("bookmarkSize", bookmarkList.size());
					
					RequestDispatcher rd = request.getRequestDispatcher("/page/bookmark.jsp"); 
					rd.forward(request, response);
				}  
			}finally{ 
				pm.close(); 
			}
			
		}else{
			response.sendRedirect("/login?mode=" + mode + "&cid=" + cid + "&eid=" + eid + "&eOrder=" + eOrder + "&imgNum=" + imgNum);
		}
		
	}
	
}
